package com.cn.dream_order_server.service.impl;

import com.cn.dream_order_server.client.GoodServer;
import com.cn.dream_order_server.mapper.OrderOrderdetailsMapper;
import com.cn.dream_order_server.pojo.GoodCommoditySku;
import com.cn.dream_order_server.pojo.OrderOrderdetails;
import com.cn.dream_order_server.pojo.OrderOrderdetailsExample;
import com.cn.dream_order_server.service.OrderOrderdetailsService;
import com.cn.dream_order_server.util.JsonUtils;
import com.cn.dream_order_server.vo.Cartvo;
import com.cn.dream_order_server.vo.GoodCommoditySkuAndSpuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class OrderOrderdetailsServiceImpl implements OrderOrderdetailsService {

    @Autowired
    private OrderOrderdetailsMapper orderOrderdetailsMapper;

    @Autowired
    private GoodServer goodServer;


    @Override
    public List<OrderOrderdetails> getOrderOrderdetailsListByOrderId(String orderId) {



        //定义条件
        OrderOrderdetailsExample example = new OrderOrderdetailsExample();
        OrderOrderdetailsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        //查询数据
        List<OrderOrderdetails> list = orderOrderdetailsMapper.selectByExample(example);
        //遍历集合将商品信息存入集合中
        for (OrderOrderdetails orderOrderdetails:list) {
            String cartVo = goodServer.getCartVo(orderOrderdetails.getCommodityId());
            Cartvo cartvo = JsonUtils.stringToObj(cartVo, Cartvo.class);
            GoodCommoditySkuAndSpuVO vo = new GoodCommoditySkuAndSpuVO(cartvo.getCommodityId(),cartvo.getSku().getSpuId(),cartvo.getSku().getTitle(),cartvo.getSku().getImages(),cartvo.getSku().getPrice(),cartvo.getSku().getOwnSpec(),cartvo.getSku().getEnable(),cartvo.getCommoditySubtitle());

            orderOrderdetails.setGoodCommoditySkuAndSpuVO(vo);
        }


        return list;
    }

    @Override
    @Transactional
    public void saveOrderOrderdetails(List<OrderOrderdetails> orderOrderdetailsList) {
        for (OrderOrderdetails orderOrderdetails:orderOrderdetailsList) {
            orderOrderdetailsMapper.insertSelective(orderOrderdetails);
        }
    }
}
